<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <input type="text" v-model="searchName">
    <ul>
      <li v-for="(item, index) in filterPersons" :key="item.id">
        {{item.id}}--{{item.name}}--{{item.age}}
      </li>
    </ul>
    <button @click="sortType=2">按年龄升序</button>
    <button @click="sortType=3">按年龄降序</button>
    <button @click="sortType=1">原本顺序</button>
  </div>
  <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.12/vue.js"></script>
  <script>
    new Vue({
      el: '#app',
      data: {
        searchName: '',
        persons: [
          {id: 1,name: 'Tom', age: 18},
          {id: 2,name: 'zs', age: 19},
          {id: 3,name: 'ls', age: 23},
          {id: 4,name: 'ww', age: 15},
          {id: 5,name: 'zl', age: 36},
        ],
        sortType: 1 //1: 不排序 2: 升序 3: 降序
      },
      computed: {
        filterPersons() {
          //1. 取出依赖数据
          const {searchName, persons, sortType} = this
          //2. 计算产生一个新数据
          const arr = persons.filter((item, index) => {
            return item.name.indexOf(searchName) !== -1
          })
          if(sortType !== 1) {
            arr.sort((p1, p2) => {
              if(sortType === 3) { //降序
                return p2.age - p1.age
              } else { //升序
                return p1.age - p2.age
              }
            })
          }
          //3. 返回新数据
         return arr 
        }
      }
    })
  </script>
</body>
</html>